package com.ruoyi.hazard.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysDeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.hazard.mapper.HazardRiskWorkMapper;
import com.ruoyi.hazard.domain.HazardRiskWork;
import com.ruoyi.hazard.service.IHazardRiskWorkService;
import org.springframework.util.CollectionUtils;

/**
 * 作业风险Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-10-25
 */
@Service
@Slf4j
public class HazardRiskWorkServiceImpl implements IHazardRiskWorkService 
{
    @Autowired
    private HazardRiskWorkMapper hazardRiskWorkMapper;
    @Autowired
    private ISysDeptService sysDeptService;

    /**
     * 查询作业风险
     * 
     * @param id 作业风险主键
     * @return 作业风险
     */
    @Override
    public HazardRiskWork selectHazardRiskWorkById(Long id)
    {
        return hazardRiskWorkMapper.selectHazardRiskWorkById(id);
    }

    /**
     * 查询作业风险列表
     * 
     * @param hazardRiskWork 作业风险
     * @return 作业风险
     */
    @Override
    public List<HazardRiskWork> selectHazardRiskWorkList(HazardRiskWork hazardRiskWork)
    {
        return hazardRiskWorkMapper.selectHazardRiskWorkList(hazardRiskWork);
    }

    /**
     * 新增作业风险
     * 
     * @param hazardRiskWork 作业风险
     * @return 结果
     */
    @Override
    public int insertHazardRiskWork(HazardRiskWork hazardRiskWork)
    {
        hazardRiskWork.setCreateTime(DateUtils.getNowDate());
        return hazardRiskWorkMapper.insertHazardRiskWork(hazardRiskWork);
    }

    /**
     * 修改作业风险
     * 
     * @param hazardRiskWork 作业风险
     * @return 结果
     */
    @Override
    public int updateHazardRiskWork(HazardRiskWork hazardRiskWork)
    {
        hazardRiskWork.setUpdateTime(DateUtils.getNowDate());
        return hazardRiskWorkMapper.updateHazardRiskWork(hazardRiskWork);
    }

    /**
     * 批量删除作业风险
     * 
     * @param ids 需要删除的作业风险主键
     * @return 结果
     */
    @Override
    public int deleteHazardRiskWorkByIds(Long[] ids)
    {
        return hazardRiskWorkMapper.deleteHazardRiskWorkByIds(ids);
    }

    /**
     * 删除作业风险信息
     * 
     * @param id 作业风险主键
     * @return 结果
     */
    @Override
    public int deleteHazardRiskWorkById(Long id)
    {
        return hazardRiskWorkMapper.deleteHazardRiskWorkById(id);
    }

    @Override
    public String importData(List<HazardRiskWork> hazardRiskWorks, Boolean updateSupport) {
        if (CollectionUtils.isEmpty(hazardRiskWorks))
        {
            throw new ServiceException("导入作业风险不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (HazardRiskWork vo : hazardRiskWorks){
            try
            {
                SysDept dept = sysDeptService.selectDeptByDeptName(vo.getDeptName());
                if(StringUtils.isNull(dept)){
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、单位 " + vo.getDeptName() + " 不存在");
                }else if(SecurityUtils.getDeptId() !=100L && dept.getParentId() != SecurityUtils.getDeptId()) {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、非单位 " + vo.getDeptName() + "人员，不可导入");
                }else if(StringUtils.isBlank(vo.getName()) || StringUtils.isBlank(vo.getRiskLevel())) {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、信息不完整，不可导入");
                }else {
                    // 验证是否存在这个隐患
                    vo.setDeptId(dept.getDeptId());
                    vo.setCreateBy(SecurityUtils.getUsername());
                    if (vo.getId() == null) {
                        insertHazardRiskWork(vo);
                        successNum++;
                        successMsg.append("<br/>" + successNum + "、风险 " + vo.getName() + " 导入成功");
                    } else if (updateSupport) {
                        HazardRiskWork exist = selectHazardRiskWorkById(vo.getId());
                        if(null == exist) {
                            failureNum++;
                            failureMsg.append("<br/>" + failureNum + "、风险 " + vo.getName() + " 更新不存在");
                        } else {
                            updateHazardRiskWork(vo);
                            successNum++;
                            successMsg.append("<br/>" + successNum + "、风险 " + vo.getName()  + " 更新成功");
                        }
                    } else {
                        failureNum++;
                        failureMsg.append("<br/>" + failureNum + "、风险 " + vo.getName() + " 已存在");
                    }
                }

            }catch (Exception e){
                log.error("导入风险数据失败",e);
                failureNum++;
                String msg = "<br/>" + failureNum + "、隐患描述 " + vo.getName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
